package com.kafka0102.demo;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

@Sharable
public class MessageServerHandler extends SimpleChannelUpstreamHandler {

	private static final Logger logger = Logger.getLogger(MessageServerHandler.class.getName());

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
		logger.log(Level.INFO, "message server handler message received.");
		if (!(e.getMessage() instanceof String)) {
			return;//(1)
		}
		String msg = (String) e.getMessage();
		System.out.println("got msg:" + msg);
		e.getChannel().write("hi,"+msg);//(2)
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		logger.log(Level.WARNING, "Unexpected exception from downstream.", e.getCause());
		e.getChannel().close();
	}
}
